<?php

namespace App\Http\Model;

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;

class Help extends Base
{
    protected $table = 'help';
    protected $primaryKey = 'help_id';
    protected $redisChannelRoom = 'CHANNEL:ROOM';

    //根据渠道跳转房间，房间的前缀
    const CHANNEL_ROOM_PRE_LIST = [
        'OPEN' => 1,//个播的前缀 => type 类型
    ];


    public function __construct(array $attributes = [])
    {
        parent::__construct($this->table, $attributes);
    }

    /**
     * 列表分页
     * @param array $condition
     * @param int $page 页数 默认0
     * @param int $limit 条数 默认10
     * @return array
     * */
    public function getHelpList($condition =[], $page = 0, $limit = 10, $app = "yyaudio")
    {
        $db = DB::table($this->table)->where($condition);

        // 总条数
        $totalCount = $db->count($this->primaryKey);

        // 查询数据
        $list = $db->offset($page * $limit)->limit($limit)->get()->toArray();
        $list = self::objToArray($list);

        foreach ($list as $key => $value) {
            if (!empty($value['data_image'])) {
                $list[$key]['data_image'] = build_image($value['data_image']);
            }
            if ($app == "yyaudio") {
                $list[$key]['url'] = build_url("/app/help/index.html?id=".$value['help_id'].'&cache=1');
            } else {
                $list[$key]['url'] = build_url("/app/llhelp/index.html?id=".$value['help_id'].'&cache=1');
            }
        }

        return $this->checkPageData($page, $limit, $totalCount, $list);
    }

    /**
     * 根据ID获取
     *
     * @param integer $id ID
     * @return array
     * */
    public function getDataById($id)
    {
        $data = [];
        do {
            $db = DB::table($this->table);
            $getData = $db->where($this->primaryKey, $id)->get()->toArray();
            $data = self::objToArray($getData);

        } while (false);

        return $data;
    }

    /**
     * 创建/更新/删除
     *
     * @param array $data 数据
     * @param string $type 操作类型 默认 insert=创建，update=更新
     * @param int $propEventId ID
     * @return bool
     * */
    public function CUDHelp($data = [], $type = 'insert', $propEventId = 0)
    {
        switch ($type) {
            case 'insert':
                $data['created_at'] = time();
                $ret = DB::table($this->table)->insertGetId($data);
                $propEventId = $ret;
                break;
            case 'update':
                $ret = DB::table($this->table)->where([$this->primaryKey => $propEventId])->update($data);
                break;
            case 'delete':
                $ret = DB::table($this->table)->where([$this->primaryKey => $propEventId])->delete();
                break;
            default:
                $ret = false;
                break;
        }

        if ($ret and $propEventId > 0 and Redis::exists($this->redisKeyProEvent . $propEventId)) {
            Redis::DEL($this->redisKeyProEvent . $propEventId);
        }

        return $ret;
    }

    /**设置渠道号指定跳转的房间
     * @param $channelNo
     * @param $val
     */
    public function setChannelRoomId($channelNo,$val)
    {
        Redis::HSET($this->redisChannelRoom, $channelNo, $val);
    }

    /**根据渠道号获取指定跳转房间
     * @param $channelNo
     * @return int
     */
    public function getRoomIdByChannel($channelNo)
    {
        $room_id =  Redis::HGET($this->redisChannelRoom, $channelNo);
        return empty($room_id) ? 0 :$room_id;
    }

    /**
     * 获取渠道号跳转房间list
     */
    public function getChannelRoomIdList()
    {
        return Redis::HGETALL($this->redisChannelRoom);
    }

    public function delChannelRoom($channel_no)
    {
        return Redis::HDEL($this->redisChannelRoom,$channel_no);
    }
}
